package pers.qianyu.module.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.core.comm.HttpResult;
import pers.qianyu.module.core.domain.system.dto.SysRoleResourceDTO;
import pers.qianyu.module.core.domain.system.vo.SysResourceVO;
import pers.qianyu.module.core.domain.system.vo.SysRoleVO;
import pers.qianyu.module.system.service.SysRoleResourceService;

import java.util.List;

/**
 * @author mizzle rain
 * @date 2021-05-03 23:24
 */
@Api(tags = "角色-资源关联接口")
@RestController
@RequestMapping("/system-serv")
public class SysRoleResourceController {
    @Autowired
    private SysRoleResourceService sysRoleResourceService;
    @ApiOperation("根据角色id查询该角色所有的资源信息")
    @GetMapping("/role_resource/role/{id}")
    public HttpResult<List<SysResourceVO>> queryByRoleId(@PathVariable("id") Long roleId) {
        List<SysResourceVO> sysResourceVOS = sysRoleResourceService.queryByRoleId(roleId);
        return HttpResult.okWithData(sysResourceVOS);
    }

    @ApiOperation("根据资源id查询拥有该资源的所有角色")
    @GetMapping("/role_resource/resource/{id}")
    public HttpResult<List<SysRoleVO>> queryByResourceId(@PathVariable("id") Long resourceId) {
        List<SysRoleVO> sysRoleVOList = sysRoleResourceService.queryByResourceId(resourceId);
        return HttpResult.okWithData(sysRoleVOList);
    }

    @ApiOperation("给角色赋权")
    @PostMapping("/role_resource")
    public HttpResult<?> insert(@RequestBody SysRoleResourceDTO sysRoleResourceDTO) {
        sysRoleResourceService.insRoleResource(sysRoleResourceDTO);
        return HttpResult.okWithMessage("角色资源新增成功");
    }

    @ApiOperation("删除角色的某一个资源")
    @DeleteMapping("/role_resource")
    public HttpResult<?> delete(@RequestBody SysRoleResourceDTO sysRoleResourceDTO) {
        sysRoleResourceService.delRoleResource(sysRoleResourceDTO);
        return HttpResult.okWithMessage("角色资源删除成功");
    }
}
